home *** CD-ROM | disk | FTP | other *** search
- /*
- * fileops.c - routines for misc file/folder operations
- */
- #include "RevRdist.h"
- #include "dispatch.h"
- #include <TransSkelProto.h>
- #include <TransDisplayProto.h>
-
-
-
- /*
- *=========================================================================
- * createFolder (name, vol, dir, ct) - create a folder
- * entry: name = string giving folder name
- * vol = ioVRefNum of volume to create folder in
- * dir = dirID name is relative to
- * ct = pointer to optional catalog node whose window information
- * will be copied to the new folder
- *=========================================================================
- */
- OSErr
- createFolder (name, vol, dir, ct)
- StringPtr name;
- Integer vol;
- Longint dir;
- cnode_t * ct;
- {
- int error;
- HParamBlockRec hpb;
- CInfoPBRec ci;
-
- error = 0;
- ZERO (hpb);
- hpb.fileParam.ioNamePtr = name;
- hpb.fileParam.ioVRefNum = vol;
- hpb.fileParam.ioDirID = dir;
- error = PBDirCreate (&hpb, false);
- if (error)
- {
- Clue1 = (SP) "\pPBDirCreate";
- goto errexit;
- }
- if (ct)
- {
- ZERO (ci);
- ci.dirInfo.ioVRefNum = vol;
- ci.dirInfo.ioFDirIndex = -1;
- ci.dirInfo.ioDrDirID = hpb.fileParam.ioDirID;
- error = PBGetCatInfo (&ci, false);
- if (error)
- {
- Clue1 = (SP) "\pPBGetCatInfo";
- goto errexit;
- }
- ci.dirInfo.ioDrCrDat = ct->crDate;
- ci.dirInfo.ioDrMdDat = ct->mdDate;
- ci.dirInfo.ioDrUsrWds.frRect = ct->in.d.dinfo.frRect;
- ci.dirInfo.ioDrUsrWds.frView = ct->in.d.dinfo.frView;
- ci.dirInfo.ioDrFndrInfo.frScroll = ct->in.d.frScroll;
- error = PBSetCatInfo (&ci, false);
- if (error)
- {
- Clue1 = (SP) "\pPBSetCatInfo";
- errexit:
- ClueID = error;
- Clue0 = (SP) "\pcreateFolder";
- }
- }
- return error;
- }
-
-
- /*
- *=========================================================================
- * discard (cp,flg) - delete file/folder from client
- * entry: cp = pointer to client catalog list node
- * flg = true to log the discard
- * ClientVol applies to catalog
- * returns: 0 on success, else OSErr
- *=========================================================================
- */
- OSErr
- discard (cp, flg)
- register cnode_t * cp;
- Boolean flg;
- {
- OSErr error;
- HParamBlockRec pb; /* PBHDelete param block */
-
- if (flg)
- notice (L_DISCARD, cp->name, nil);
- if (Flags & DB_LISTONLY)
- return 0;
- /*
- * If the file is locked, we need to unlock it first
- */
- if (cp->attrib & fLocked)
- error = unlock (cp);
- ZERO (pb);
- pb.fileParam.ioNamePtr = cp->name;
- pb.fileParam.ioVRefNum = ClientVol;
- pb.fileParam.ioDirID = cp->parID;
- error = PBHDelete (&pb, false);
- if (error && error != fnfErr)
- {
- ClueID = error;
- if (flg)
- notice (L_SYS, (SP) "\pdiscard", (SP) "\pPBDelete", nil);
- }
- else
- if (flg)
- statMsgClr ();
- return error;
- }
-
-
-
-
- /*
- *=========================================================================
- * emptyFolder (ap, func) - empty client folder given by catalog list entry
- * entry: first param = pointer to catalog list node for client folder
- * second param = pointer to function to call for each entry
- * in folder to see if it should be deleted.
- * = nil to always delete
- * ClientVol = VRefNum of client volume
- * returns: 0 if folder emptied, else <> 0
- *=========================================================================
- */
- DISPATCHED (emptyFolder)
- {
- struct lm
- {
- frame_t f;
- cnode_t * ap; /* copy of first arg */
- ProcPtr func; /* copy of second arg */
- cnode_t * fp; /* head of catalog list for folder */
- cnode_t * cp; /* ptr to current entry in fp */
- Boolean rej; /* true once filter rejects */
- };
- typedef struct lm lm_t;
- register lm_t * m;
- register cnode_t * cp; /* working copy of m->cp */
- OSErr error;
- Ptr paramv[2];
- short result;
-
- m = *(lm_t **)fh;
- result = request;
- error = 0;
- switch (request)
- {
- case R_INIT:
- if (ClueID = resizeFrame (fh, sizeof (lm_t)))
- return R_ERROR;
- m = *(lm_t **)fh;
- m->ap = (cnode_t *)argv[0];
- m->func = (ProcPtr)argv[1];
- m->f.state = 1;
- return R_CONT;
-
- case R_CONT:
- switch (m->f.state)
- {
- case 1:
- notice (L_DOEMPTY, m->ap->name, nil);
- if (m->ap->attrib & fLocked)
- {
- if (error = unlock (m->ap))
- {
- paramv[0] = (Ptr)error;
- return (popCall (R_CONT, paramv));
- }
- }
- cp = listFolder (ClientVol, m->ap->dirID);
- if (cp == nil)
- {
- paramv[0] = 0;
- return (popCall (R_CONT, paramv));
- }
- m->fp = m->cp = cp;
- m->f.state = 2;
- return R_CONT;
- case 2:
- /*
- * For each iteration in state 2, we discard one entry,
- * either a file or a folder
- */
- cp = m->cp;
- if (!cp)
- break; /* done with folder, exit */
- m->cp = cp->link; /* assume will go on */
- if (m->func && !(*m->func)(cp))
- {
- m->rej = true; /* if filter rejects entry */
- return R_CONT;
- }
- if (cp->ctype == C_FOLDER)
- {
- /*
- * Use ourselves to empty a folder
- */
- m->cp = cp; /* back up to us again */
- m->f.state = 3;
- paramv[0] = (Ptr) cp;
- paramv[1] = (Ptr) m->func;
- result = pushCall (emptyFolder, paramv);
- if (result == R_CONT)
- Depth++;
- return result;
- }
- error = discard(cp, true);
- if (error && error != fnfErr)
- m->rej = true;
- return R_CONT;
- case 3:
- /*
- * Here after folder emptied
- */
- Depth--;
- cp = m->cp;
- m->cp = cp->link;
- m->f.state = 2;
- if (argv[0])
- m->rej = true; /* folder could not be emptied */
- else
- {
- if ((error = discard (cp, true)) && error != fnfErr)
- m->rej = true;
- }
- return R_CONT;
- } /* end of R_CONT state switch */
- break;
- /*
- * R_QUIT and R_BACKOUT just fall through
- */
- }
- paramv[0] = nil;
- if (GetHandleSize ((Handle)fh) >= sizeof (lm_t))
- {
- freeList (m->fp);
- paramv[0] = (Ptr)&m->rej;
- }
- statMsgClr ();
- return (popCall (result, paramv));
- }
-
-
-
- /*
- *=========================================================================
- * relock (cp) - set the finder locked bit on a client file/folder
- * entry: cp = pointer to catalog list entry for file/folder
- * ClientVol set
- * returns: OSErr
- *=========================================================================
- */
- OSErr
- relock (cp)
- register cnode_t * cp;
- {
- HParamBlockRec pb;
- OSErr error;
-
- ZERO (pb);
- pb.fileParam.ioNamePtr = cp->name;
- pb.fileParam.ioVRefNum = ClientVol;
- pb.fileParam.ioDirID = cp->parID;
- error = PBHSetFLock (&pb, false);
- if (error && error != fnfErr)
- {
- Clue1 = (SP) "\pPBHSetFLock";
- Clue0 = (SP) "relock";
- return error;
- }
- cp->attrib |= fLocked;
- return error;
- }
-
-
-
- /*
- *=========================================================================
- * unlock (cp) - clear the finder locked bit on a client file/folder
- * entry: cp = pointer to catalog list entry for file/folder
- * ClientVol set
- * returns: OSErr
- *=========================================================================
- */
- OSErr
- unlock (cp)
- register cnode_t * cp;
- {
- HParamBlockRec pb;
- OSErr error;
-
- ZERO (pb);
- pb.fileParam.ioNamePtr = cp->name;
- pb.fileParam.ioVRefNum = ClientVol;
- pb.fileParam.ioDirID = cp->parID;
- error = PBHRstFLock (&pb, false);
- if (error && error != fnfErr)
- {
- Clue1 = (SP) "\pPBHRstFLock";
- Clue0 = (SP) "unlock";
- return error;
- }
- cp->attrib &= ~fLocked;
- return error;
- }
-
-
- /*
- *=========================================================================
- * verifyBlessed () - check client "Blessed" folder
- * entry: ClientVol set
- *=========================================================================
- */
- void
- verifyBlessed ()
- {
- OSErr error;
- Longint dirID; /* dir ID of blessed folder */
- long saveflags; /* copy of Flags */
- CInfoPBRec ci; /* for PBGetCatInfo */
- HParamBlockRec pb; /* for PBHGetVInfo */
- Str255 fs, vs; /* file and volume name strings */
-
- /*
- * Get info about client volume.
- */
- Clue0 = (SP)"\pverifyBlessed";
- ZERO (pb);
- vs[0] = 0;
- pb.volumeParam.ioNamePtr = vs;
- pb.volumeParam.ioVRefNum = ClientVol;
- pb.volumeParam.ioVolIndex = -1;
- Clue1 = (SP)"\pPBHGetVInfo";
- if (error = PBHGetVInfo (&pb, false))
- goto syserr;
- dirID = pb.volumeParam.ioVFndrInfo[0];
- if (dirID == 0)
- {
- /*
- * No blessed folder set yet. See if there is one with right name
- */
- fs[0] = 0;
- GetIndString (fs, NAME_STR, NAME_FOLDER);
- if (fs[0] == 0)
- return;
- ZERO (ci);
- ci.dirInfo.ioNamePtr = fs;
- ci.dirInfo.ioVRefNum = ClientVol;
- Clue1 = (SP)"\pPBGetCatInfo";
- if (error = PBGetCatInfo (&ci, false))
- goto cantboot;
- dirID = ci.dirInfo.ioDrDirID;
- /*
- * Make it the Blessed folder, which may be presumptuous
- */
- if (Flags & DB_VERBOSE)
- notice (L_BLESSED, nil);
- if ((Flags & DB_LISTONLY) == 0)
- {
- pb.volumeParam.ioVFndrInfo[0] = dirID;
- pb.volumeParam.ioVFndrInfo[1] = dirID;
- pb.volumeParam.ioNamePtr = 0;
- Clue1 = (SP)"\pPBSetVInfo";
- if (error = PBSetVInfo (&pb, false))
- goto syserr;
- }
- }
- /*
- * Check Blessed folder for System file and Finder
- */
- fs[0] = 0;
- GetIndString (fs, NAME_STR, NAME_SYSTEM);
- if (fs[0] == 0)
- return;
- ZERO (ci);
- ci.hFileInfo.ioNamePtr = fs;
- ci.hFileInfo.ioVRefNum = ClientVol;
- ci.hFileInfo.ioDirID = dirID;
- Clue1 = (SP)"\pPBGetCatInfo";
- if (error = PBGetCatInfo (&ci, false))
- goto cantboot;
- fs[0] = 0;
- GetIndString (fs, NAME_STR, NAME_FINDER);
- if (fs[0] == 0)
- return;
- ci.hFileInfo.ioDirID = dirID;
- ci.hFileInfo.ioFDirIndex = 0;
- if (error = PBGetCatInfo (&ci, false))
- goto cantboot;
- pb.volumeParam.ioNamePtr = 0;
- (void) PBFlushVol ((ParmBlkPtr)&pb, false);
- return;
-
- cantboot:
- if (error == fnfErr)
- {
- saveflags = Flags;
- Flags &= ~DB_STARTUP;
- warning (E_BLESSED, vs, fs, nil);
- Flags = saveflags;
- return;
- }
- syserr:
- ClueID = error;
- panic (true, E_SYS, nil);
- }